package com.frikinet.lifeguard.tests;

import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.lobobrowser.html.UserAgentContext;
import org.lobobrowser.html.gui.HtmlPanel;
import org.lobobrowser.html.parser.DocumentBuilderImpl;
import org.lobobrowser.html.parser.InputSourceImpl;
import org.lobobrowser.html.test.SimpleHtmlRendererContext;
import org.lobobrowser.html.test.SimpleUserAgentContext;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class VelocityHtmlTest
{
	public static void main(String args[]) throws SAXException, IOException
	{
		// sets log level
		Logger.getLogger("org.lobobrowser").setLevel(Level.WARNING);

		UserAgentContext uaContext = new SimpleUserAgentContext();
		DocumentBuilderImpl buider = new DocumentBuilderImpl(uaContext);

		Velocity.init();
		VelocityContext vc = new VelocityContext();
		vc.put("name", "Paco");
		StringWriter writer = new StringWriter();
		Velocity.mergeTemplate("test.vm", "ISO-8859-1", vc, writer);
		writer.close();
		StringReader reader = new StringReader(writer.toString());		
		InputSourceImpl input = new InputSourceImpl(reader, new File(".").toURI().toString());
		Document doc = buider.parse(input);
		reader.close();

		HtmlPanel panel = new HtmlPanel();
		panel.setDocument(doc, new SimpleHtmlRendererContext(panel, uaContext));

		final JFrame frame = new JFrame("Hello world!");
		frame.getContentPane().add(panel);

		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				frame.pack();
				frame.setVisible(true);
			}
		});
	}
}
